10. Scope Example

Scope Librarian Example

Scope Example

TIP: The JavaScript language is constantly improving. One of these updates introduces a new type of scope, called Block scope. Check out our ES6 course to learn more!

Scope 1

Which of these variables a, b, c, or d, is defined in the global scope?

var a = 1;
function x() {
  var b = 2;
  function y() {
    var c = 3;
    function z() {
      var d = 4;
    }
    z();
  }
  y();
}

x();
SOLUTION: a

Scope 2

Where can you print out the value of variable c without resulting in an error?

var a = 1;
function x() {
  var b = 2;
  function y() {
    var c = 3;
    function z() {
      var d = 4;
    }
    z();
  }
  y();
}

x();
SOLUTION:
  • anywhere inside function `y()`
  • anywhere inside function `z()`